package org.example.java.io;


import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 有了内存映射文件，你就可以认为文件已经全部读进了内存，然后把它当成一个非常大的数组来访问了。
 * Created by Juntao.Zhang on 2014/5/20.
 */
public class LargeMappedFiles {
  static int length = 0x8FFFFFF; // 128 Mb

  public static void main(String[] args) throws Exception {
    MappedByteBuffer out =
        new RandomAccessFile("G:\\document\\data\\test.dat", "rw").getChannel()
            .map(FileChannel.MapMode.READ_WRITE, 0, length);
//    for (int i = 0; i < length; i++)
//      out.put((byte) 'x');
//    System.out.println("Finished writing");

//    for (int i = length / 2; i < length / 2 + 6; i++)
//      System.out.print((char) out.get(i));

  }

}
